﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KnitwearCompany.Models
{
    public class Cart
    {
        public List<PurchaseItem> Purchases = new List<PurchaseItem>();

        public decimal GetSum()
        {
            decimal sum = 0;
            foreach (var item in Purchases)
                sum += item.GetSum();
            return sum;
        }

        public void AddPurchase(Product product, int amount)
        {
            var purchase = new PurchaseItem();
            purchase.Amount = amount;
            purchase.Product = product;
            Purchases.Add(purchase);
        }

        public void AddPurchase(PurchaseItem purchase)
        {
            Purchases.Add(purchase);
        }

        public void RemovePurchase(Product product)
        {
            PurchaseItem removedPurchase = null;
            foreach (var item in Purchases)
                if (item.Product.Id == product.Id)
                    removedPurchase = item;
            if (removedPurchase != null)
                Purchases.Remove(removedPurchase);
        }
    }
}